#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>

#define DRIVER_NAME "hello"
#define NODE_NAME "hello_device_node"

MODULE_LICENSE("Dual BSD/GPL"); // required

static long hello_fs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    printk("cmd is %d, arg is %ld\n", cmd, arg);
    return 0;
}
static int hello_fs_release(struct inode *inode, struct file *file)
{
    printk(KERN_EMERG "hello_fs_release");
    return 0;
}
static int hello_fs_open(struct inode *inode, struct file *file)
{
    printk(KERN_EMERG "hello_fs_open");
    return 0;
}
static struct file_operations hello_fops = {
    .owner = THIS_MODULE,
    .open = hello_fs_open,
    .release = hello_fs_release,
    .unlocked_ioctl = hello_fs_ioctl,
};
static struct miscdevice hello_miscdevice = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = NODE_NAME,
    .fops = &hello_fops,
};
static int hello_probe(struct platform_device *p)
{
    printk(KERN_EMERG "\nhello_probe\n");
//    misc_register(&hello_miscdevice);
    return 0;
}

static int hello_remove(struct platform_device *p)
{
    printk(KERN_EMERG "\nhello_remove\n");
    misc_deregister(&hello_miscdevice);
    return 0;
}

static void hello_shutdown(struct platform_device *p)
{
    printk(KERN_EMERG "\nhello_shutdown\n");
}

static int hello_suspend(struct platform_device *p, pm_message_t state)
{
    printk(KERN_EMERG "\nhello_suspend\n");
    
    return 0;
}

static int hello_resume(struct platform_device *p)
{
    printk(KERN_EMERG "\nhello_resume\n");
    
    return 0;
}

static struct platform_driver hello_driver={
    .probe = hello_probe,
    .remove = hello_remove,
    .shutdown = hello_shutdown,
    .suspend = hello_suspend,
    .resume = hello_resume,
    .driver = {
        .name = DRIVER_NAME,
        .owner = THIS_MODULE,
    },
};

static int hello_init(void) //insmod xxx.ko, execute it
{
    printk(KERN_EMERG "\nhello world enter~\n\n");
    platform_driver_register(&hello_driver);
    misc_register(&hello_miscdevice);
    return 0;    
}

static void hello_exit(void) //rmmod xxx( note: not xxx.ko ), execute it
{
    printk(KERN_EMERG "\nhello world exit~\n\n");
    platform_driver_unregister(&hello_driver);
}



module_init(hello_init);
module_exit(hello_exit);

